from flask import Flask

from application import db

class User(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	username = db.Column(db.String(80), unique=True)
	passwd =  db.Column(db.String(80))
	email = db.Column(db.String(120), unique=True)
	posts = db.relationship('Post', backref=db.backref('post', lazy='joined'),lazy='dynamic')
	def __init__(self, username, passwd, email=None):
		self.username = username
		self.email = email
		self.passwd = passwd

	def __repr__(self):
		return '<User %r>' % self.username
		
class Category(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(50), unique=True)
	posts = db.relationship('Post', backref=db.backref('posts', lazy='joined'),lazy='dynamic')
	def __init__(self, name):
		self.name = name
		
	def __repr__(self):
		return '<Category %r>' % self.name

tags = db.Table('tags',
	db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
	db.Column('post_id', db.Integer, db.ForeignKey('post.id'))
)

class Tag(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(50), unique=True)

class Post(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	title = db.Column(db.String(80))
	body =  db.Column(db.String(1000))
	permalink = db.Column(db.String(80))
	pub_date = db.Column(db.DateTime)
	category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
	tags = db.relationship('Tag', secondary=tags,backref=db.backref('posts', lazy='dynamic'))
	user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

	def __init__(self,title,body,permalink,pub_date,category_id,tags):
		self.title = title
		self.body = body
		self.permalink = permalink
		self.pub_date = pub_date
		self.category_id = category_id
		self.tags = tags

	def __repr__(self):
		return '<Post %r>' % self.title
		

